/**
 * Filename: StatusDetialAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/13 11:02
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.UserStatusActivity;
import com.github.postsanf.yinian.bean.YNComment;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.SnackBarUtils;
import com.github.postsanf.yinian.utils.TimeUtils;
import com.github.postsanf.yinian.utils.YNUrlHelper;

import java.util.ArrayList;
import java.util.List;

public class CardStatusDetialAdapter extends RecyclerView.Adapter<CardStatusDetialAdapter.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private List<YNComment> mDatas = new ArrayList<>();
    private View mHeaderView;
    private Context mContext;
    private int groupid;
    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public CardStatusDetialAdapter(Context mContext, List<YNComment> mDatas, int groupid) {
        this.mDatas = mDatas;
        this.mContext = mContext;
        this.groupid = groupid;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout ll_comment;
        SimpleDraweeView comment_user_avatar;
        TextView comment_user_name;
        TextView comment_time;
        TextView comment_content;

        public ViewHolder(View itemView) {
            super(itemView);
            if (itemView == mHeaderView) return;
            ll_comment = (LinearLayout) itemView
                    .findViewById(R.id.ll_comment);
            comment_user_avatar = (SimpleDraweeView) itemView
                    .findViewById(R.id.comment_user_avatar);
            comment_user_name = (TextView) itemView
                    .findViewById(R.id.comment_user_name);
            comment_time = (TextView) itemView
                    .findViewById(R.id.comment_time);
            comment_content = (TextView) itemView
                    .findViewById(R.id.comment_content);
        }

        public void bindData(final YNComment listItems) {
            if (listItems != null) {
                if (listItems.getCommentedUser().getUserid() != CommonConstants.SYSTEM_ID) {
                    String toSb = "回复" + listItems.getCommentedUser().getUnickname() + ": " + listItems.getCcontent();
                    Spannable WordtoSpan = new SpannableString(toSb);
                    WordtoSpan.setSpan(new TextAppearanceSpan(mContext, R.style.item_comment_text_style3), 2, 2 + listItems.getCommentedUser().getUnickname().length() + 1,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    comment_content.setText(WordtoSpan);
                } else {
                    comment_content.setText(listItems.getCcontent());
                }
                ImageOptHelper.showFrescoAvatar(Uri.parse(YNUrlHelper.getAvatarUrl(mContext, listItems.getCommentUser().getUpic())), comment_user_avatar, mContext);
                comment_user_name.setText(listItems.getCommentUser().getUnickname());
                comment_time.setText(TimeUtils.getGroupStatusTime(listItems.getCtime()));
                // 点击用户头像或昵称跳转
                comment_user_avatar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        lookUser(comment_user_avatar, listItems);
                    }
                });
                ll_comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null) {
                            mListener.OnItemClick(v, listItems);
                        }
                    }
                });
            }
        }
    }

    private void lookUser(View view, YNComment item) {
        if (groupid > 0) {
            Intent intent = new Intent(mContext, UserStatusActivity.class);
            intent.putExtra(CommonConstants.YNUSERID, item.getCommentUser().getUserid());
            intent.putExtra(CommonConstants.YNGROUPID, groupid);
            mContext.startActivity(intent);
        } else {
//			ToastUtils.showToast(mContext, "个人页面无法查看评论用户详情", Toast.LENGTH_SHORT);
            SnackBarUtils.makeShort(view, "个人页面无法查看评论用户详情").show();
        }
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    private YNComment getItem(int position) {
        return mDatas.get(position);
    }

    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) return new ViewHolder(mHeaderView);
        final View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_card_comment, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(holder);
        YNComment listItems = getItem(pos);
        holder.bindData(listItems);
        holder.itemView.setTag(listItems);
    }

    public interface OnItemClickListener {
        public void OnItemClick(View view, YNComment data);
    }


    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? mDatas.size() : mDatas.size() + 1;
    }
}
